trivial-httpd: Add --force-range-requests option to force range requests
authorJeremy Whiting <jpwhiting@kde.org>
Wed, 14 Aug 2013 20:08:25 +0000 (14:08 -0600)
committerColin Walters <walters@verbum.org>
Wed, 14 Aug 2013 21:33:23 +0000 (17:33 -0400)
This will be used to test resuming interrupted downloads for
ostree-pull.

With this option, if a whole file is asked for, only half of the file
is given.  Then the client should retry with a range request, and
we'll give them the other half.

https://bugzilla.gnome.org/show_bug.cgi?id=705925

src/ostree/ot-builtin-trivial-httpd.c

index d6f85f5d2d69ead77b433035cd701264ef348020..f431eff9eb8e35833c6140e8bf36669eca5b2ecc 100644 (file)
@@ -29,6 +29,7 @@
 static char *opt_port_file = NULL;
 static gboolean opt_daemonize;
 static gboolean opt_autoexit;
+static gboolean opt_force_ranges;
 
 typedef struct {
   GFile *root;
@@ -39,6 +40,7 @@ static GOptionEntry options[] = {
   { "daemonize", 'd', 0, G_OPTION_ARG_NONE, &opt_daemonize, "Fork into background when ready", NULL },
   { "autoexit", 0, 0, G_OPTION_ARG_NONE, &opt_autoexit, "Automatically exit when directory is deleted", NULL },
   { "port-file", 'p', 0, G_OPTION_ARG_FILENAME, &opt_port_file, "Write port number to PATH", "PATH" },
+  { "force-range-requests", 0, 0, G_OPTION_ARG_NONE, &opt_force_ranges, "Force range requests by only serving half of files", NULL },
   { NULL }
 };
 
@@ -199,6 +201,10 @@ do_get (OtTrivialHttpd  *self,
         {
           GMappedFile *mapping;
           SoupBuffer *buffer;
+          gsize buffer_length, file_size;
+          SoupRange *ranges;
+          int ranges_length;
+          gboolean have_ranges;
 
           mapping = g_mapped_file_new (safepath, FALSE, NULL);
           if (!mapping)
@@ -207,8 +213,17 @@ do_get (OtTrivialHttpd  *self,
               goto out;
             }
 
+          file_size = g_mapped_file_get_length (mapping);
+          have_ranges = soup_message_headers_get_ranges(msg->request_headers, file_size, &ranges, &ranges_length);
+          if (opt_force_ranges && !have_ranges)
+            buffer_length = file_size/2;
+          else
+            buffer_length = file_size;
+
+          if (have_ranges)
+            soup_message_headers_free_ranges (msg->request_headers, ranges);
           buffer = soup_buffer_new_with_owner (g_mapped_file_get_contents (mapping),
-                                               g_mapped_file_get_length (mapping),
+                                               buffer_length,
                                                mapping, (GDestroyNotify)g_mapped_file_unref);
           soup_message_body_append_buffer (msg->response_body, buffer);
           soup_buffer_free (buffer);